﻿
/*
 * Javasript based on Functions(JBF) 1.03
 * 
 * Copyright(c) 2010, Fooxee (JA86.CN)
 *
 * JBF is licensed under the terms of the Open Source GPL 3.0(http://www.gnu.org/licenses/gpl.html) license. 
 * 
 * JBF's blog: http://www.cnblogs.com/JBF/
 * 
 * Created by Fooxee. at 2010.03.08  mailTo:hjccjun@163.com
 */

//DOM相关操作
JBF.Dom = JBF.dom = function(selector,context)
{
	return new JBF.Dom.fn.init(selector,context);
};

JBF.Dom.fn={
	init : function(selector,context)
	{
		if (selector.nodeType) 
		{
			this[0] = selector;
			this.el = this[0];
			return this;
		}
		if (typeof selector == "string" )
		{
			return JBF(document.getElementById(selector));
		}
		return this;
	},
	getId : function()
	{
		return this.el.id;
	},
	//隐藏
	hide : function()
	{
		var disattr=this.el.style.display;
		this.setAttrs({'JBF-DOM-DISPLAY' : JBF.isEmpty(disattr) ? '' : disattr});
		this.el.style.display='none';
		return this;
	},
	//显示
	show : function()
	{
		var disattr=this.getAttr('JBF-DOM-DISPLAY');
		if(JBF.isEmpty(disattr))
		{
			this.el.style.display = '';
		}
		else
		{
			this.el.style.display = (disattr == 'none' ? '' : disattr);
			this.removeAttr('JBF-DOM-DISPLAY');
		}
		return this;
	},
	//设置属性
	setAttrs:function(v)
	{
		v=JBF.isObject(v)?v:{};
		var el=this.el;
		JBF.each(v,function(p,idx)
		{
			el.setAttribute(p,v[p]);
		});
		return this;
	},
	//取得属性值
	getAttr:function(s)
	{
		if(JBF.isEmpty(s))
		{
			return null;	
		}
		return this.el.getAttribute(s);
	},
	//移除属性
	removeAttr:function(s)
	{
		if(JBF.isEmpty(s))
		{
			return;	
		}
		this.el.removeAttribute(s);
	},
	currentStyle:function()
	{
		return this.el.currentStyle || document.defaultView.getComputedStyle(this.el, null);
	},
	offset:function(el)
	{
		var left = 0, top = 0, elem = el || this.el;
		left=elem.offsetParent ? elem.offsetLeft + this.offset(elem.offsetParent).left  : elem.offsetLeft;
		top =elem.offsetParent ? elem.offsetTop + this.offset(elem.offsetParent).top : elem.offsetTop;
		return {top:top,left:left};
	},
	//css
	css:function(v)
	{
		if(JBF.isPrimitive(v) || JBF.isObject(v))
		{
			if(JBF.isObject(v))
			{
				var cs='';
				JBF.each(v,function(v,i,list)
				{
					cs +=v+':'+list[v]+' '
				});
				this.cssText(cs);
			}
			else
			{
				this.el.className=v;
			}
			return this;
		}
		else
		{
			return this.el.className;
		}
	},
	cssText:function(s)
	{
		if(JBF.isPrimitive(s))
		{
			this.el.style.cssText=s;
		}
		return this;
	},
	//html
	html:function(html)
	{
		if(JBF.isPrimitive(html))
		{
			this.el.innerHTML=html;
			return this;
		}
		else
		{
			return this.el.innerHTML;
		}
	},
	//value
	val:function(v)
	{
		if(JBF.isPrimitive(v))
		{
			this.el.value=v;
			return this;
		}
		else
		{
			return this.el.value;	
		}
	},
	wrap:function()
	{
		
	},
	addEvent : function(evtname, handler,capture)
	{
		var el=this.el, thisObj=this;
		handler = JBF.isFunction(handler) ? handler : JBF.emptyFn;
		capture = !JBF.isBoolean(capture) ? false : capture;
		
		if (el.attachEvent)
		{
			el.attachEvent("on"+evtname, function(event){handler(thisObj, event || window.event, this);});
		}
		else if(el.addEventListener)
		{
			el.addEventListener(evtname, function(event){handler(thisObj, event || window.event, this);}, capture);
		}
		else 
		{
			el['on'+v] = function(event){handler(thisObj, event || window.event, this);};
		}
	},
	addEvents:function(listeners,capture)
	{
		listeners=JBF.isObject(listeners)?listeners:{};
		JBF.each(listeners,function(v,i,list)
		{
			this.addEvent(v,list[v],capture);
		});
		return this;
	},
	//此方法是 addEvent 的简写形式 是等价的
	on:function(evtname, handler,capture)
	{
		return this.addEvent(evtname, handler,capture);
	},
	
	//这两个方法 no!!!!!
	removeEvent : function(evtname, handler,capture)
	{
		var el=this.el, thisObj=this;
		handler = JBF.isFunction(handler) ? handler : JBF.emptyFn;
		capture = !JBF.isBoolean(capture) ? false : capture;
		
		if (el.detachEvent)
		{
			el.detachEvent('on'+evtname,function(event){handler(thisObj, event || window.event, this);});
		}
		else if(el.removeEventListener)
		{
			el.removeEventListener(evtname, function(event){handler(thisObj, event || window.event, this);}, capture);
		}
		else 
		{
			el['on'+v] = null;
		}
	},
	removeEvents:function(listeners,capture)
	{
		listeners=JBF.isObject(listeners)?listeners:{};
		JBF.each(listeners,function(v,i,list)
		{
			this.removeEvent(v,list[v],capture);
		});
		return this;
	},
	
	//未完善
	fireEvent : function(t)
	{
		if(JBF.isIE)
		{
			this.el.fireEvent('on'+t);
		}
		else
		{
			var evt = (document.createEvent("MouseEvents"));  
         	evt.initEvent(t,true,true);
			this.el.dispatchEvent(evt);
			/*http://www.w3school.com.cn/xmldom/met_document_createevent.asp
			HTMLEvents HTMLEvent iniEvent() 
MouseEvents MouseEvent iniMouseEvent() 
UIEvents UIEvent iniUIEvent() 

			*/
		}
	},
	click : function()
	{
		this.fireEvent('click');
		return this;
	},
	focus : function()
	{
		this.fireEvent('focus');
		return this;
	},
	autoTestPwdStrength:function(outResultDivId,minPwdLen)
	{
		var pwd=this.val();
		var strength = 0;
		minPwdLen=JBF.isNumber(minPwdLen)?minPwdLen:6;
		if (pwd.length<minPwdLen && pwd.length>1)
		{
			strength = 1;
		}
		else
		{
			strength = ((pwd.search(/[a-zA-Z]/)!=-1) ? 1 : 0) + ((pwd.search(/[0-9]/)!=-1) ? 1 : 0) + ((pwd.search(/[^A-Za-z0-9_]/)!=-1) ? 1 : 0);
		}
		
		if(!JBF.isEmpty(outResultDivId))
		{
			var spanCss="border:1px solid #ccc;color:#999; border-right:none; padding:2px 10px 0px 10px; margin:0px; float:left;",current="background:#E3F1B8 none repeat scroll 0 0;font-weight:bold;";
			var levL=spanCss+(strength==1?current:''),
				levM=spanCss+(strength==2?current:''),
				levH=spanCss+'border:1px solid #ccc;color:#999;'+(strength==3?current:'');
			var html='<span style="margin:0px; padding:0px;">';
			html+='<span style="'+levL+'">低</span><span style="'+levM+'">中</span>';
			html+='<span style="'+levH+'">高</span>';
			html+='</span>';
			JBF.Dom(outResultDivId).html(html);
		}
		return strength;
	}
}
JBF.Dom.fn.init.prototype=JBF.Dom.fn;

//元素element 1 
//属性attr 2 
//文本text 3 
//注释comments 8 
//文档document 9 

JBF.apply(JBF.Dom,{  
	
	//创建浮动DIV
	createFloatDivWrap:function(innerHtml,cssText,id)
	{
		return "<div id='"+(JBF.isEmpty(id)?('JBF-'+(JBF.idSeed++)) :id)+"' style='position:absolute;z-index:2;width:100%;height:100%;left:0;top:0;background:#fff;"+(cssText?cssText:'')+"' >"+innerHtml+"</div><iframe frameborder='0' border='0' style='width:100%;height:100%;position:absolute;z-index:1;left:0;top:0;filter:Alpha(opacity=0);'></iframe>";
	},
	
	//ready  
	//private
	readyCache : [],
	readyTimer : false,
	
	isReady : false,
	ready : function(handler)
	{
		this.pollReady();
		if(!JBF.isFunction(handler))
		{
			return;
		}
		if(this.isReady)
		{
			handler();
		}
		else
		{
			this.readyCache.push(handler);	
		}
	},
	//private
	pollReady : function()
	{
		if(arguments.callee.one)
		{
			return;
		}
		arguments.callee.one = true;
		//var fire=JBF.Dom.fireReady;
		//alert(fire)
		//ie
		if(JBF.isIE && self === self.top)
		{
			(function(){
				if (JBF.Dom.isReady) return;
				try
				{document.documentElement.doScroll("left");} catch(e)
				{
					JBF.Dom.readyTimer=setTimeout(arguments.callee,0);
					return;
				}
				JBF.Dom.ready();
			})();
			document.onreadystatechange = function()
			{
				if (document.readyState === 'complete')
				{
					document.onreadystatechange = null;//清除加载函数
					JBF.Dom.fireReady();
				}
			};
		}
		else
		{
			document.addEventListener("DOMContentLoaded", function()
			{
				document.removeEventListener( "DOMContentLoaded", arguments.callee, false );//清除加载函数
				JBF.Dom.fireReady();
			}, false);
		}
	},
	fireReady : function()
	{
		JBF.Dom.isReady=true;
		JBF.each(JBF.Dom.readyCache,function(v,i,list)
		{
			list[v].call(window);
		});
		if (JBF.Dom.readyTimer){window.clearTimeout(JBF.Dom.readyTimer);}
	}
		   
});
//重写JBF包中的ready相关方法和字段
JBF.isReady=JBF.Dom.isReady;
JBF.ready=function(hander)
{
	JBF.Dom.ready(hander);	
};
